module(..., package.seeall) -- 使得文件中的函数在何处都可调用

require "nvm"
require "io"
require "ntp"
require "logGenerator"
nvm.init("config.lua", true)
local LOG_DIR_PATH, fileName, logInfo, errorInfo, sysTime = "/sysLog_dir", "",
                                                            "", "", ""

-- 预警设备参数设置-身份判别
--[[ local function containClientsOrNot(clients)
    do
        local containResult = false
        for i = 1, #clients do
            if clients[i] == nvm.get("info")[1] then
                containResult = true
            end
        end
        log.info("containResult", containResult)
        return containResult
    end
end
 ]]
--[[
    函数名：getDateNum(timeNow, timeNext)
    功能：计算当前日期与之前日期的差值
    参数：当前日期，之前日期
    返回值：日期差值                     ]]
local function getDateNum(timeBefore)
    local ret = 0
    if timeBefore then
        -- log.info("nowyear", os.date("*t").year, "nowmoth", os.date("*t").month, "nowday", os.date("*t").day)
        local num1 = os.time({
            year = os.date("*t").year,
            month = os.date("*t").month,
            day = os.date("*t").day
        })
        local num2 = os.time({
            year = tonumber(string.split(timeBefore, "-")[1]),
            month = tonumber(string.split(timeBefore, "-")[2]),
            day = tonumber(string.split(timeBefore, "-")[3])
        })
        if num1 and num2 then ret = math.abs(num1 - num2) / (3600 * 24) end
    end
    return ret
end

--[[
    delAllExpiredFiles()
    功能：读取指定目录下的所有文件，并删除日期大于2个月的文件
    参数：无
    返回值：无                     ]]
--[[ function delAllExpiredFiles(tjsondata)
    logInfo, errorInfo = "", ""
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
        os.date("*t").month, os.date("*t").day, os.date("*t").hour, os.date("*t").min, os.date("*t").sec)
    if tjsondata["clients"] then
        if containClientsOrNot(tjsondata["clients"]) then
            if io.exists(LOG_DIR_PATH) then
                if rtos.remove_dir(LOG_DIR_PATH) then
                    logInfo = logInfo .. "[" .. sysTime .. "]" .. "[日志文件删除成功！]\n"
                else
                    errorInfo = errorInfo .. "[" .. sysTime .. "]" .. "[日志文件删除失败！]\n"
                end
            else
                errorInfo = errorInfo .. "[" .. sysTime .. "]" .. "[日志文件目录不存在，未执行删除操作！]\n"
            end
            if logInfo ~= "" and logInfo ~= false then
                logGenerator.generateLogTask(logInfo, "log")
            end
            if errorInfo ~= "" and errorInfo ~= false then
                logGenerator.generateLogTask(errorInfo, "log")
            end
        end
    end
end
 ]]
--[[
    函数名：delExpiredFiles(date)
    功能：读取指定目录下的所有文件，并删除日期大于2个月的文件
    参数：当前日期
    返回值：无                     ]]
function delExpiredFiles()
    local tm = misc.getClock()
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
                            os.date("*t").month, os.date("*t").day,
                            os.date("*t").hour, os.date("*t").min,
                            os.date("*t").sec)
    if rtos.get_fs_free_size() < 921600 then
        if rtos.remove_dir(LOG_DIR_PATH) then
            logInfo = logInfo .. "[" .. sysTime .. "]" ..
                          "[日志文件删除成功！]\n"
            logGenerator.generateLogTask(logInfo, "sysLog", 0)
        else
            errorInfo = errorInfo .. "[" .. sysTime .. "]" ..
                            "[日志文件删除失败！]\n"
            logGenerator.generateLogTask(errorInfo, "sysLog", 0)
        end
    end

    if tm.hour == 0 then
        local fileType, fileSize, preDate, d_value, dateNow
        if io.opendir(LOG_DIR_PATH) then
            while true do
                fileType, fileName, fileSize = io.readdir()
                if not fileName then break end
                preDate = string.sub(fileName, -14, -5)
                d_value = getDateNum(preDate)
                if d_value >= 60 then
                    os.remove(LOG_DIR_PATH .. "/" .. fileName)
                    logInfo = logInfo .. "[" .. sysTime .. "]" .. fileName ..
                                  "日志文件删除成功!\n"
                end
            end
            io.closedir()
        else
            print("目录不存在或者输入目录地址有误")
        end
    end
end

sys.timerLoopStart(delExpiredFiles, 1000)
